package org.fessingapore.library.controllers;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.fessingapore.library.models.Group;
import org.fessingapore.library.models.Member;
import org.fessingapore.library.services.GroupService;
import org.fessingapore.library.services.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/member")
public class MemberController {

	private MemberService memberService;
	private GroupService groupService;

	@Autowired
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	@Autowired
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@InitBinder
	public void bind(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				try {
					setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
					return;
				} catch (ParseException e) {
				}
				try {
					setValue(new SimpleDateFormat("yyyy").parse(value));
					return;
				} catch (ParseException e) {
				}
				setValue(null);
			}
			public String getAsText() {
				if (getValue() == null)
					return null;
				else
					return new SimpleDateFormat("yyyy-MM-dd")
							.format((Date) getValue());
			}

		});
	}
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String searchAll(Model model) {
		List<Group> groupList = groupService.findAll();
		List<Member> memberList = memberService.findAll();
		model.addAttribute("groupList", groupList);
		model.addAttribute("memberList", memberList);
		model.addAttribute("member", new Member());
		return "member/searchMember";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addMember(Model model) {
		List<Group> groupList = groupService.findAll();
		model.addAttribute("groupList", groupList);
		model.addAttribute("member", new Member());
		model.addAttribute("action", "/add");
		return "member/addMember";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String createMember(Member member, Model model) {
		memberService.create(member);
		return "redirect:/member";
	}
	
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String viewMember(@PathVariable String id, Model model) {
		List<Group> groupList = groupService.findAll();
		model.addAttribute("groupList", groupList);
		
		if (!id.equals("")){
			Member member = memberService.getById(Integer.parseInt(id));
			model.addAttribute("member", member);
		}
		
		return "member/viewMember";
	}
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editMember(@PathVariable String id, Model model) {
		List<Group> groupList = groupService.findAll();
		model.addAttribute("groupList", groupList);
		
		if (!id.equals("")){
			Member member = memberService.getById(Integer.parseInt(id));
			model.addAttribute("member", member);
		}
		model.addAttribute("action", "/update");
		return "member/addMember";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateMember(Member member, Model model) {
		memberService.update(member);
		return "redirect:/member";
	}

	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteMember(@PathVariable String id, Model model) {
		
		if (!id.equals("")){
			Member member = memberService.getById(Integer.parseInt(id));
			memberService.delete(member);
		}
		return "redirect:/member";
	}

	
	
}
